package com.hjm.common.utils;


import com.hjm.project.bankExpenditure.bankExpenditure.domain.PosOrder;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.*;
import java.math.BigDecimal;
import java.net.HttpURLConnection;
import java.net.URL;

/**
 * 签购单生成信息
 */
public class SignPicOrder {
    // 签名宽度
    private static final int WIDTH = 337;
    // 签名高度
    private static final int HEIGHT = 92;
    /**
     * 导出图片新
     * @param posinfo 电子小票信息
     * @param bginputStream 背景图
     * @param signImg 签名图片
     * @param needCompress 是否需要压缩签名图片 path 转换之后保存图片
     */
    public static void exportImg(PosOrder posinfo, InputStream bginputStream, String signImg, boolean needCompress, String path, boolean needqianzi, String postype) throws Exception{
        HttpURLConnection httpUrl = null;
        URL url = null;
        BufferedInputStream bis = null;
        InputStream inputStream=null;
        OutputStream os=null;
        try{
            BufferedImage buffImg = ImageIO.read(bginputStream);
            //得到画笔对象
            Graphics graphics = buffImg.getGraphics();
            //如果有签名或者类型是Mpos的  需要把签名加上
            if(needqianzi || "mpos".equals(postype)){
                if("mpos".equals(postype)){
                    //远程下载签名
                    url = new URL(signImg);
                    httpUrl = (HttpURLConnection) url.openConnection();
                    httpUrl.connect();
                    inputStream=httpUrl.getInputStream();
                }else{
                    //获取签名图片
                    inputStream=new FileInputStream(signImg);
                }

                bis = new BufferedInputStream(inputStream);
                //转换图片流
                BufferedImage small = ImageIO.read(bis);
                //绘制签名固定坐标位置
                int width = small.getWidth(null);
                int height = small.getHeight(null);
                int smallX = 0; //绘制签名图片X坐标
                int smallY = 532; //绘制签名图片Y坐标
                if (needCompress) { // 压缩LOGO
                    //压缩的倍数 = 图片的高度 / 显示的高度
                    double multiple = div(height, HEIGHT , 2);
                    //显示的宽度 = 图片的宽度 / 压缩的倍数
                    double widthdiv = div(width, multiple , 0);
                    int showWidth = (int) widthdiv;
                    if (width > showWidth) {
                        width = showWidth;
                    }
                    if(width<=120){
                        width=120;
                    }
                    if (height > HEIGHT) {
                        height = HEIGHT;
                    }
                    Image image = small.getScaledInstance(width, height,Image.SCALE_SMOOTH);
                    BufferedImage tag = new BufferedImage(width, height,BufferedImage.TYPE_INT_RGB);
                    Graphics g = tag.getGraphics();
                    g.drawImage(image, 0, 0, null); // 绘制缩小后的图
                    g.dispose();
                    small = tag;

                    //定位压缩后签名X坐标显示
                    smallX = smallX + ((WIDTH - width) / 2);
                }//27 770
                graphics.drawImage(small,27, 370, width, height, null);
            }

            //最后一个参数用来设置字体的大小
            Font f = new Font("宋体",Font.PLAIN,22);
            Color mycolor = new Color(0, 0, 0);//黑色字体
            graphics.setColor(mycolor);
            graphics.setFont(f);
            //pos签购单抬头
            graphics.drawString("银联POS签购单",70,45);//抬头
            //主体
            setThem(graphics,mycolor,2);
            //虚线
            graphics.drawString("- - - - - -商户存根- - - - - -",27,70);
            //商户名称
            setThem(graphics,mycolor,2);
            graphics.drawString("商户名称:"+(posinfo.getMerchantName()==null?"":posinfo.getMerchantName()),27,90);
            //商户编号
            setThem(graphics,mycolor,2);
            graphics.drawString("商户编号:"+(posinfo.getMerchantNo()==null?"":posinfo.getMerchantNo()),27,110);
            //终端编号
            setThem(graphics,mycolor,2);
            graphics.drawString("终端编号:"+(posinfo.getBankpos()==null?"":posinfo.getBankpos()),27,130);
            //发卡行号
            setThem(graphics,mycolor,2);
            graphics.drawString("发卡行:"+(posinfo.getBankname()==null?"":posinfo.getBankname()),27,150);
            //收单行号
            setThem(graphics,mycolor,2);
            graphics.drawString("收单行:48480000",27,170);
            //卡号
            setThem(graphics,mycolor,1);
            graphics.drawString("卡号:"+(posinfo.getPan().substring(0,6)+"******"+posinfo.getPan().substring(posinfo.getPan().length()-4))+" "+(posinfo.getCardtype()==null?"":posinfo.getCardtype()),27,190);
            //交易类型
            setThem(graphics,mycolor,1);
            graphics.drawString("交易类型:1101-消费",27,210);
            //批次号
            setThem(graphics,mycolor,2);
            graphics.drawString("批次号:000001",27,230);
            //流水号
            setThem(graphics,mycolor,2);
            graphics.drawString("流水号:"+(posinfo.getStan()==null?"":posinfo.getStan()),27,250);
            //授权码
            setThem(graphics,mycolor,2);
            graphics.drawString("授权码:"+(posinfo.getAir()==null?"":posinfo.getAir()),27,270);
            //参考号
            setThem(graphics,mycolor,2);
            graphics.drawString("参考号:"+(posinfo.getRrno()==null?"":posinfo.getRrno()),27,290);
            //日期和时间
            setThem(graphics,mycolor,1);
            graphics.drawString("日期时间:"+(posinfo.getRiqi()==null?"":posinfo.getRiqi()),27,310);
            //交易金额
            setThem(graphics,mycolor,1);
            graphics.drawString("金额:"+(posinfo.getAmount()==null?"   RMB "+"0.00":"   RMB "+posinfo.getAmount()),27,330);

            //操作员号
            setThem(graphics,mycolor,2);
            graphics.drawString("操作员号:01",27,350);

            //持卡人签名
            setThem(graphics,mycolor,2);
            graphics.drawString("持卡人签名",27,370);
            //虚线
            setThem(graphics,mycolor,2);
            graphics.drawString("- - - - - - - - - - - - - - -",27,470);
            //商户存根
            setThem(graphics,mycolor,2);
            graphics.drawString("本人确认以上交易同意记入本卡账户 ",27,510);
            setThem(graphics,mycolor,2);
            graphics.drawString("客服热线：400-023-3388转4",27,530);



            graphics.dispose();

            //使用临时文件上传
            String shareFileName = path;
            os = new FileOutputStream(shareFileName);

            ImageIO.write(buffImg, "jpg", os);

            os.flush();

        }catch (Exception e){
           throw e;
        }finally {
            if(os!=null){
                os.close();
            }
            if(null!=bginputStream){
                bginputStream.close();
            }
            if(null!=inputStream){
                inputStream.close();
            }
            if(null!=bis){
                bis.close();
            }
            if(httpUrl!=null){
                httpUrl.disconnect();
            }
        }
    }

    //设置主题信息
    public static void setThem(Graphics graphics,Color color,int them){
        //标题
        if(them==1){
            Color mycolor = new Color(0, 0, 0);//浅黑色
            Font f = new Font("宋体",Font.PLAIN|Font.BOLD,14);
            graphics.setFont(f);
            graphics.setColor(mycolor);
        }else if(them==2){
            //Color mycolor = new Color(80, 80, 78);//浅黑色
            Color mycolor = new Color(0, 0, 0);//浅黑色
            Font f = new Font("宋体",Font.PLAIN,14);
            graphics.setFont(f);
            graphics.setColor(mycolor);
        }else if(them==3){
            //Color mycolor = new Color(80, 80, 78);//浅黑色
            Color mycolor = new Color(0, 0, 0);//浅黑色
            Font f = new Font("宋体",Font.PLAIN|Font.BOLD,13);
            graphics.setFont(f);
            graphics.setColor(mycolor);
        }else{
            //显示字段
            Color mycolor = new Color(0,0,0);//黑色大点
            Font f = new Font("宋体",Font.PLAIN|Font.BOLD,14);
            graphics.setFont(f);
            graphics.setColor(mycolor);
        }
    }

    public static double div(double v1, double v2, int scale) {
        if (scale < 0) {
            throw new IllegalArgumentException(
                    "The scale must be a positive integer or zero");
        }
        BigDecimal b1 = new BigDecimal(Double.toString(v1));
        BigDecimal b2 = new BigDecimal(Double.toString(v2));

        return b1.divide(b2, scale, 4).doubleValue();
    }
}
